# -*- coding: utf-8 -*-


from bs4 import BeautifulSoup  # bs4模块

html_doc = """
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
    <p class="title"><b>The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>

    <p class="story">...</p>
"""

# 创建对象  BeautifulSoup(网页源代码， 解析器)
soup = BeautifulSoup(html_doc, 'lxml') # html5lib
# print(type(soup), soup)

# 解析数据   <title>The Dormouse's story</title>
# 先定位标签
# print(soup.title)

# 定位head标签
# print(soup.head.title.string)

# 获取文字内容 The Dormouse's story
# print(soup.title.string)

# 需求: 获取a标签下的href属性值
# print(soup.a['href'])  # 默认只获取第一个元素
# print(soup.a['class'])
# print(soup.a.attrs)  # attrs返回出来的是字典
# print(soup.a.attrs['href'])  # attrs返回出来的是字典

"""
find(): 只找一个
find_all(): 找所有
返回的数据类型: 列表， 可以通过循环遍历
"""
# a = soup.find_all('a')
# print(a)

# a = soup.find('a')  # 只找第一个
# print(a)

# 可导航的字符串
# title_tag = soup.title
# print(title_tag.string[0])

# 获取子标签
# head_tag = soup.head  # 定位到head
# print(head_tag.contents)  # 返回的数据类型是列表
# 迭代器  next()  for
# lst = head_tag.children # <list_iterator object at 0x0000011F7FAE07F0> # 迭代器
# for i in lst:
#     print(i)

# 获取子孙标签内容
# lst = head_tag.descendants
# for i in lst:
#     print(i)

head_tag = soup.head  # 定位到head
"""
涉及到多个节点的内容，无法确定是哪一个内容，返回的None
"""
# print(head_tag.string)
# print(head_tag.strings)  # 通过上级标签，也可以直接获取子级标签的文本内容
data = head_tag.stripped_strings  # 可以删除多余的空行
for d in data:
    print(d)